package com.jeeplus.modules.guide.service;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.PageBean;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.guide.entity.*;
import com.jeeplus.modules.guide.mapper.MobileIndexMapper;
import com.jeeplus.modules.guide.utils.DataUtils;
import com.jeeplus.modules.sys.entity.MobileUserQR;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@RequiredArgsConstructor
@Slf4j
@Service
@Transactional(readOnly = true)
public class MobileIndexService {
    private final MobileSilentService mobileSilentService;
    private final MobileIndexMapper mobileIndexMapper;
    public List<Map<String,Object>> getMatterTop(String version){
        return mobileIndexMapper.getMatterTop(version);
    }

    public List<Map<String,Object>> findMatterList(String label,String version){
        return mobileIndexMapper.findMatterList(label,version);
    }

    public List<Map<String,String>> findZNDBMatterList(String label,String version){
        if(StringUtils.isNotBlank(label))label = "%"+label+"%";
        return mobileIndexMapper.findZNDBMatterList(label,version);
    }

    public Map<String,Object> getMatterBSZN(String oid, String version,String otherVersion){
        Map<String,Object> map = null;
        if("2".equals(otherVersion)){
            map = mobileIndexMapper.getMatterBSZNEN(oid,version);
        }else{
            map = mobileIndexMapper.getMatterBSZN(oid,version);
        }
        if(null != map){
            if(map.containsKey("handleForm")){
                if("1".equals(map.get("handleForm"))){
                    map.put("handleForm","网上办理");
                }else if("2".equals(map.get("handleForm"))){
                    map.put("handleForm","窗口、网上均可办理");
                }else {
                    map.put("handleForm","窗口办理");
                }
            }
            if(map.containsKey("legalLimit")){
                map.put("legalLimit",map.get("legalLimit")+"个工作日");
            }
            if(map.containsKey("promiseLimit")){
                map.put("promiseLimit",map.get("promiseLimit")+"个工作日");
            }
            if(map.containsKey("locationAddr") && null != map.get("locationAddr")){
                map.put("locationAddr", JSONArray.parseArray(map.get("locationAddr").toString()));
            }

        }
        return map;
    }

    public String getMatterMaterialList (String oid,String version){
        return mobileIndexMapper.getMatterMaterialList(oid,version);
    }
    /**
     * 获取事项办理状态
     * getMatterType
     * @return 受影响的行数
     */
    public Map<String,Integer> getMatterType (@Param("oid")String oid,@Param("version")String version){
        return mobileIndexMapper.getMatterType(oid,version);
    }

    /**
     * 获取事项网上办理集合
     * getMatterTypeValue
     * @return 受影响的行数
     */
    public List<Map<String,String>> getMatterTypeValue (String oid,String version){
        return mobileIndexMapper.getMatterTypeValue(oid,version);
    }


    /**
     * 获取事项部门
     * getMatterManger
     * @return 受影响的行数
     */
    public List<Map<String,String>> getMatterManger (String version){
        return mobileIndexMapper.getMatterManger(version);
    }


    /**
     * 获取事项部门事项
     * getMatterMaterialList
     * @return 受影响的行数
     */
    public List<Map<String,String>> getMatterMangerList(String version,String organName){
        return mobileIndexMapper.getMatterMangerList(version,organName);
    }

    /**
     * 获取街道事项部门事项
     * getMatterMaterialList
     * @return 受影响的行数
     */
    public List<Map<String,String>> getMatterStreeList(String version,String organName){
        return mobileIndexMapper.getMatterStreeList(version,organName);
    }

    /**
     * 获取事项层级
     * getMatterLevel
     * @return 受影响的行数
     */
    public List<String> getMatterLevel (String version){
        return mobileIndexMapper.getMatterLevel(version);
    }

    /**
     * 获取事项层级事项集合
     * getMatterLevelList
     * @return 受影响的行数
     */
    public List<Map<String,String>> getMatterLevelList (String version,String areaType){
        return mobileIndexMapper.getMatterLevelList(version,areaType);
    }

    /**
     * 获取热点事项
     * getMatterLevelList
     * @return 受影响的行数
     */
    public List<Map<String,String>> getMatterTopList (String version){
        return mobileIndexMapper.getMatterTopList(version);
    }

    /**
     * 获取事项名称
     * getMatterName
     * @return 受影响的行数
     */
    public String getMatterName (String version,String oid){
        return mobileIndexMapper.getMatterName(version,oid);
    }

    /**
     * 主题事项
     * getMatterItem
     * @return 受影响的行数
     */
    public List<Map<String,String>> getMatterItem (String serviceSubject,String serviceLifeCycle,String version){
        return mobileIndexMapper.getMatterItem(serviceSubject,serviceLifeCycle,version);
    }

    /**
     * 获取点位事项
     * getMatterItem
     * @return 受影响的行数
     */
    public List<Map<String,String>> findAddrMatterList (String areaId,String version){
        return mobileIndexMapper.findAddrMatterList(areaId,version);
    }

    public MobileUserQR getUserValueByCode(String code){
        return mobileIndexMapper.getUserValueByCode(code);
    }


    /**
     * 老人版/英文版事项加载
     * getMatterOldOrEN
     * @return 受影响的行数
     */
    public List<Map<String,String>> getMatterOldOrEN (String serviceOldAndEN,String version){
        if("1".equals(serviceOldAndEN)){
            return mobileIndexMapper.getMatterOld(version);
        }else{
            return mobileIndexMapper.getMatterEN(version);
        }
    }

    public String findIsHaveDo(String version,String oid){
        return mobileIndexMapper.findIsHaveDo(version,oid);
    }

    public String findOtherUrl(String oid,String version){
        return mobileIndexMapper.findOtherUrl(oid,version);
    }

    public Integer isHasArea(String oid){
        return mobileIndexMapper.isHasArea(oid);
    }

    public String findIsStree(String version,String oid){
        return mobileIndexMapper.findIsStree(version,oid);
    }
    public Map<String,String> findStreetUser(String loginName){
        return mobileIndexMapper.findStreetUser(loginName);
    }
    public List<Map<String,String>> getMatterOlineByOid(String version,List<String> list){
        return mobileIndexMapper.getMatterOlineByOid(version,list);
    }

    public Boolean checkOnline(String oid,String version) {
        if(mobileIndexMapper.checkOnline(oid, version) >= 1){
            return true;
        }else return false;
    }

    @Transactional(readOnly = false)
    public Integer intoSWHistory(String matterName,String matterCode,String matterStatus,String startTime,String endTime,String ids,String gxUserId){
        if(mobileIndexMapper.updateSWHistory(matterCode,matterStatus,endTime,DateUtils.getDateTime(),ids,gxUserId) <= 0){
            return mobileIndexMapper.intoSWHistory(matterName,matterCode,matterStatus,startTime,endTime,ids,gxUserId, DateUtils.getDateTime());
        }else{
            return 1;
        }
    }

    public Integer findYMBCSCount(){
        return mobileIndexMapper.findYMBCSCount();
    }

    /**
     * 获取高馨办注册及网上取号数
     * gxbListValue
     * @return
     */
    public List<Integer> gxbListValueQD(String startTime,String endTime){
        return mobileIndexMapper.gxbListValueQD(startTime,endTime);
    }

    /**
     * 获取高馨办注册及网上取号数
     * gxbListValue
     * @return
     */
    public List<Integer> gxbListValue(String type){
        if("all".equals(type)){
            // type = all 全量
            return mobileIndexMapper.gxbListValue();
        }else if("day".equals(type)){
            // type = day 当天
            return mobileIndexMapper.gxbListValueDay();
        }else if("week".equals(type)){
            // type = week 7天
            return mobileIndexMapper.gxbListValueWeek();
        }else if("month".equals(type)){
            // type = month 30天
            return mobileIndexMapper.gxbListValueMonth();
        }else if("threeMonth".equals(type)){
            // type = threeMonth 3个月
            return mobileIndexMapper.gxbListValueThreeMonth();
        }else{
            // type = year 12月
            return mobileIndexMapper.gxbListValueYear();
        }
    }

    /**
     * 一码办成事办件统计
     * ymbcsTypeNameList
     * @return
     */
    public List<Map<String,Object>> ymbcsTypeNameList(String type){
        if("all".equals(type)){
            // type = all 全量
            return mobileIndexMapper.ymbcsTypeName();
        }else if("day".equals(type)){
            // type = day 当天
            return mobileIndexMapper.ymbcsTypeNameDay();
        }else if("week".equals(type)){
            // type = week 7天
            return mobileIndexMapper.ymbcsTypeNameWeek();
        }else if("month".equals(type)){
            // type = month 30天
            return mobileIndexMapper.ymbcsTypeNameMonth();
        }else if("threeMonth".equals(type)){
            // type = threeMonth 3个月
            return mobileIndexMapper.ymbcsTypeNameThreeMonth();
        }else{
            // type = year 12月
            return mobileIndexMapper.ymbcsTypeNameYear();
        }
    }

    /**
     * 获取高馨办访问数据
     * gxbListValue
     * @return
     */
    public List<Map<String,Object>> gxbVisitValue(String type){
        if("month".equals(type)){
            // type = month 30天
            return mobileIndexMapper.gxbVisitValueMonth();
        }else if("threeMonth".equals(type)){
            // type = threeMonth 3个月
            return mobileIndexMapper.gxbVisitValueThreeMonth();
        }else if("year".equals(type)){
            // type = year 12个月
            return mobileIndexMapper.gxbVisitValueThreeYear();
        }else{
            // 默认7天访问趋势
            return mobileIndexMapper.gxbVisitValue();
        }
    }
    /**
     * 获取一码办成事用户数据
     * gxbListValue
     * @return
     */
    public PageBean <YmbcsUser> ymbcsUserList(String pageNo, String pageSize,String type){
        if(StringUtils.isBlank(pageSize))pageSize="5";
        if(StringUtils.isBlank(pageNo))pageNo="1";
        PageBean <YmbcsUser> pageBean = new PageBean<YmbcsUser>();
        PageHelper.startPage(Integer.valueOf(pageNo) , Integer.valueOf(pageSize));
        List<YmbcsUser> list = null;
        if("all".equals(type)){
            list = mobileIndexMapper.ymbcsUserList();
        }else if("day".equals(type)){
            list = mobileIndexMapper.ymbcsUserListDay();
        }else if("week".equals(type)){
            list = mobileIndexMapper.ymbcsUserListWeek();
        }else if("month".equals(type)){
            list = mobileIndexMapper.ymbcsUserListMonth();
        }else if("threeMonth".equals(type)){
            list = mobileIndexMapper.ymbcsUserListThreeMonth();
        }else if("year".equals(type)){
            list = mobileIndexMapper.ymbcsUserListYear();
        }
        PageInfo <YmbcsUser> pageInfo = new PageInfo<YmbcsUser>(list);
        pageBean.setCrurentNum(Integer.valueOf(pageNo));
        pageBean.setPageSize(Integer.valueOf(pageSize));
        pageBean.setTotalNum((int) pageInfo.getTotal());
        pageBean.setTotalPageSize(pageInfo.getPages());
        pageBean.setDatas(pageInfo.getList());
        return pageBean;
    }





    public List<Map<String,String>> selectYMBCSList(){
        return mobileIndexMapper.selectYMBCSList();
    }

    @Transactional(readOnly = false)
    public void updateYMBCS(String serviceTime,String typeName,String areaName,String countName,String number,String registerTime,String userId,String id,String areaId){
        mobileIndexMapper.updateYMBCS(serviceTime,typeName,areaName,countName,number,registerTime,userId,id,areaId);
    }


    public Map<String,String> findRegisterTime(String idcard){
        return mobileIndexMapper.findRegisterTime(idcard);
    }

    @Transactional(readOnly = false)
    public void intoUserZZJ(String name,String idcard,String tell){
        try{
            Map <String, String> map = mobileIndexMapper.findRegisterTime(idcard);
            mobileIndexMapper.intoUserZZJ( name, idcard, tell, DataUtils.getBeforeDate(),DateUtils.getDateTime(), "自助机业务", "自助机服务", "暂无数据", "暂无数据", map.get("createTime"), 1,map.get("userId"));
        }catch (Exception e){
            log.error("自助机业务添加记录失败！");
        }

    }

    @Transactional(readOnly = false)
    public void intoUserZZQJ(String name,String idcard,String tell){
        try{
            Map <String, String> map = mobileIndexMapper.findRegisterTime(idcard);
            mobileIndexMapper.intoUserZZJ( name, idcard, tell, DataUtils.getBeforeDate(),DateUtils.getDateTime(), "自助机扫码取件", "自助机服务", "暂无数据", "暂无数据", map.get("createTime"), 1,map.get("userId"));
        }catch (Exception e){
            log.error("自助机业务添加记录失败！");
        }

    }

    @Transactional(readOnly = false)
    public void updateYMBCSCanll(String id){
        mobileIndexMapper.updateYMBCSCanll(id);
    }

    public List<YmbcsValue> ymbcsUserValueById(String userId){
        return mobileIndexMapper.ymbcsUserValueById(userId);
    }

    public Integer ymbcsMaterialNum(String userId){
        return mobileIndexMapper.ymbcsMaterialNum(userId);
    }

    public List<Map<String,Object>> matterListLinShi(){
        return mobileIndexMapper.matterListLinShi();
    }
    public String findListByName(String matterName,String version){
        return mobileIndexMapper.findListByName(matterName,version);
    }
    public List<String> ymbcsMaterialNames(String userId){
        return mobileIndexMapper.ymbcsMaterialNames(userId);
    }

    @Transactional(readOnly = false)
    public Integer delQRById(){
        return mobileIndexMapper.delQRById();
    }

    public List<MapOffices> mapOfficesList(){
        return mobileIndexMapper.mapOfficesList();
    }

    @Transactional(readOnly = false)
    public Integer insertCompany(List<Company>  list,String doTime){
        return mobileIndexMapper.insertCompany(list,doTime);
    }

    @Transactional(readOnly = false)
    public Integer delCompany(String doTime){
        return mobileIndexMapper.delCompany(doTime);
    }

    @Transactional(readOnly = false)
    public Integer backCompany(String doTime){
        return mobileIndexMapper.backCompany(doTime);
    }

    /**
     * 是否不动产事项
     * findMatterManager
     * @return
     */
    public String findMatterManager(String oid,String version){
        return mobileIndexMapper.findMatterManager(oid,version);
    }

    /**
     * 电子社保码解析
     * intoQrcode
     * @return
     */
    @Transactional(readOnly = false)
    public Integer intoQrcode(String qrcode,String result){
        return mobileIndexMapper.intoQrcode(qrcode,result);
    }


    /**
     * 一码办成事 市场监管局人脸核身取证记录 一码申请量统计 事项名称为 营业执照发放
     * intoQrcode
     * @return
     */
    @Transactional(readOnly = false)
    public void intoUserSCJGJ(String name,String idcard,String tell){
        try{
            Map <String, String> map = mobileIndexMapper.findRegisterTime(idcard);
            mobileIndexMapper.intoUserZZJ( name, idcard, tell, DataUtils.getBeforeDate(),DateUtils.getDateTime(), "营业执照遗失补领换发申请", "市场监管局", "暂无数据", "暂无数据", map.get("createTime"), 1,map.get("userId"));
        }catch (Exception e){
            log.error("营业执照发放添加记录失败！");
        }

    }

    /**
     * 一码办成事 对接街道网上办记录 一码申请量统计
     * intoQrcode
     * @return
     */
    @Transactional(readOnly = false)
    public void intoUserStreet(String name,String idcard,String tell,String matterName,String areaName){
        try{
            Map <String, String> map = mobileIndexMapper.findRegisterTime(idcard);
            mobileIndexMapper.intoUserZZJ( name, idcard, tell, DataUtils.getBeforeDate(),DateUtils.getDateTime(), matterName, areaName, "暂无数据", "暂无数据", map.get("createTime"), 1,map.get("userId"));
        }catch (Exception e){
            log.error("添加记录失败！");
        }

    }

    public List<Map<String,String>> getMatterItemOther(){
        return mobileIndexMapper.getMatterItemOther();
    }
}
